﻿package 
{
	import flash.display.Sprite;
	import flash.events.*;

	import gs.TweenLite;
	import gs.easing.*;

	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.render.BasicRenderEngine;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.view.Viewport3D;

	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.view.layer.ViewportLayer;
	import org.papervision3d.view.layer.util.ViewportLayerSortMode;
	import org.papervision3d.events.InteractiveScene3DEvent;

	public class CoverFlow extends Sprite
	{
		//- PRIVATE & PROTECTED VARIABLES -------------------------------------------------------------------------
		private var _viewport:Viewport3D;
		private var _scene:Scene3D;
		private var _camera:Camera3D;
		private var _renderer:BasicRenderEngine;

		private var container:DisplayObject3D;
		private var numItem:int;
		private var stageW:int;
		private var stageH:int;
		private var planeWidth:int;
		private var planeHeight:int;
		private var spaceX:int;

		private var point:int;
		private var arrLayer:Array;
		//- PUBLIC & INTERNAL VARIABLES ---------------------------------------------------------------------------
		//- CONSTRUCTOR -------------------------------------------------------------------------------------------
		public function CoverFlow():void
		{
			init();
		}
		//- PRIVATE & PROTECTED METHODS ---------------------------------------------------------------------------
		private function init():void
		{
			//initVars
			stageW=stage.stageWidth;
			stageH=stage.stageHeight;
			planeWidth=360;
			planeHeight=250;
			arrLayer=[];
			spaceX=100;
			//initMethod
			initPapervision();
			initObjects();
			initListeners();
			//exe
			container.getChildByName("plane"+0).dispatchEvent(new InteractiveScene3DEvent(InteractiveScene3DEvent.OBJECT_CLICK))
		}

		private function initPapervision():void
		{
			_viewport = new Viewport3D(stageW, stageH);
			_viewport.interactive=true;
			_scene = new Scene3D();
			_camera = new Camera3D();
			_renderer = new BasicRenderEngine();
			container=new DisplayObject3D();
			addChild(this._viewport);
			_scene.addChild(container);

			_camera.focus=100;
			_camera.zoom=10;
			_camera.z=-1000;
		}

		private function initObjects():void
		{
			numItem=7;
			_viewport.containerSprite.sortMode = ViewportLayerSortMode.INDEX_SORT;
			for (var i:int; i<numItem; i++) {
				var color=new ColorMaterial(Math.random()*0xffffff, 1, true);
				var bmp=new BitmapMaterial(new Img0(360,250))
				var bmp1=new BitmapMaterial(new Img1(373,302))
				bmp.interactive=true;
				bmp.smooth=true;
				bmp1.interactive=true;
				bmp1.smooth=true;
				if(i==0){
				var plane=new Plane(bmp,planeWidth, planeHeight, 3,3);
				}else if(i==1){
					var plane=new Plane(bmp1,planeWidth, 302, 3,3);
					plane.y=-20
				}else{
					var plane=new Plane(color,planeWidth, planeHeight, 3,3);
					}
				plane.name="plane"+i;
				plane.x=i*spaceX;
				plane.rotationY=-50;
				//event
				_viewport.buttonMode=true;
				plane.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK , fnClick);
				container.addChild(plane);

				var layer:ViewportLayer=_viewport.containerSprite.getChildLayer(plane);
				layer.layerIndex=i;
				arrLayer[i]=layer;
			}
			// init scene objects here
		}

		private function initListeners():void
		{
			addEventListener(Event.ENTER_FRAME, render);
		}

		private function render($evt:Event):void
		{
			// animation code goes here
			_renderer.renderScene(this._scene, this._camera, this._viewport);
		}
		//- PUBLIC & INTERNAL METHODS -----------------------------------------------------------------------------
		//- EVENT HANDLERS ----------------------------------------------------------------------------------------
		private function fnClick(e:Event):void
		{
			point=int(e.target.name.substr(5));
			var plane:Plane=e.target as Plane;
			for (var i:int; i<numItem; i++) {
				if (i==point) {
					arrLayer[i].layerIndex=numItem;
					TweenLite.to(container.getChildByName("plane"+i),0.5,{z:0,x:i*spaceX, rotationX:0,rotationY:0, ease:Cubic.easeOut});
					//
					TweenLite.to(container,0.5,{x:point*-spaceX, ease:Cubic.easeOut});
				} else {
					arrLayer[i].layerIndex=i;
					if (i>point) {
						arrLayer[i].layerIndex=numItem-i;
						TweenLite.to(container.getChildByName("plane"+i),0.5,{z:100,x:i*spaceX+50, rotationY:50, ease:Cubic.easeOut});
					} else {
						arrLayer[i].layerIndex=i;
						TweenLite.to(container.getChildByName("plane"+i),0.5,{z:100,x:i*spaceX-50, rotationY:-50, ease:Cubic.easeOut});
					}
				}
			}
		}
		//- GETTERS & SETTERS -------------------------------------------------------------------------------------
		//- HELPERS -----------------------------------------------------------------------------------------------
		override public function toString():String
		{
			return "CoverFlow";
		}
		//- END CLASS ---------------------------------------------------------------------------------------------
	}
}